<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\library\Ems;
use app\common\library\Sms;
use fast\Random;
use think\Config;
use think\Validate;
use app\common\library\WxBizDataCrypt;
use wxpay\WeixinPay;
use app\admin\model\card\Code as CardCodeModel;
use app\admin\model\card\Log as CardLogModel;
use app\admin\model\card\Agreement as CardAgreementModel;
use app\admin\model\card\Explain as CardExplainModel;
use app\common\model\User as UserModel;
use app\admin\model\agent\Log as AgentLogModel;



/**
 * 会员接口
 */
class VipCard extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';
    
    
    public function getAgerment()
    {
        $data = CardAgreementModel::field('content')->find();
        $this->success('获取成功',$data);
    }
    
    public function getNotice()
    {
        $data = CardExplainModel::field('content')->find();
        $this->success('获取成功',$data);
    }
    
    
    public function getInfo()
    {
        if($this->auth->is_agent == 0)
        {
            $this->error('获取失败',[]);
        }
        //获取全部
        $where['agent_id'] = $this->auth->id;
        $return['total'] = CardCodeModel::where($where)->count();
        //获取以激活$card_where['is_exchange'] = '1';
        $where['is_exchange'] = '1';
        $return['acticve']  = CardCodeModel::where($where)->count();
        $this->success('获取成功',$return);
    }
    
    public function getVipCard()
    {
        if($this->auth->is_agent == 0)
        {
            $this->error('获取失败',[]);
        }
        $type = $this->request->post('type','');
        $where = [];
        if($type != '')
        {
            $where['is_exchange'] = $type;
        }
        $where['agent_id'] = $this->auth->id;
        $page = $this->request->post("page",1);
        $start = 0;
        if($page > 1)
        {
           $start = ($page - 1)*15;
        }
        //limit($start,15)->
        $list = CardCodeModel::alias("a")->join("user b","a.user_id = b.id",'left')->field("a.*,b.nickname")->where($where)->order('create_time desc')->select();
        if($list)
        {
            foreach($list as $k => $v)
            {
                if($v['is_exchange'] == 1)
                {
                    $list[$k]['use_time'] = date('Y-m-d H:i:s',$v['use_time']);
                }
            }
        }
        $this->success('获取成功',$list);
    }
    
    public function batchCreateVipCard()
    {
        if($this->auth->is_agent == 0)
        {
            $this->error('暂未获得生成权限',[]);
        }
        $number = $this->request->post('number',10);
        $code = $this->request->post('code','');
        if(!$number)
        {
            $this->error('请输入生成数量',[]);
        }
        $str = '';
        
        
        
        for($i=0;$i<$number;$i++)
        {
            $car_no = date('Ymd').rand(111111,999999);
            $code = Random::alnum(8);
            $ins_data[] = ['card' => $car_no, 'code' =>$code , 'agent_id' => $this->auth->id, 'create_time' => time()];
            $str .= "序号：".($i+1).",卡号：$car_no,卡密：$code 。\r\n";
        }
        $status = CardCodeModel::insertAll($ins_data);
        if($status)
        {
            $this->success('生成成功',['data'=>$str]);
        }
        $this->error('生成失败，请重试');
    }
    
    public function createVipCard()
    {
        if($this->auth->is_agent == 0)
        {
            $this->error('暂未获得生成权限',[]);
        }
        $card = $this->request->post('card','');
        $code = $this->request->post('code','');
        if(!$card || !$code)
        {
            $this->error('请输入卡号、卡密',[]);
        }
        $check_where['card'] = $card;
        $check_where['agent_id'] = $this->auth->id;
        $sttaus=  CardCodeModel::where($where)->find();
        if($sttaus)
        {
            $this->error('卡号已存在',[]);
        }
        $ins_data = ['card' => $card, 'code' => $code, 'agent_id' => $this->auth->id, 'create_time' => time()];
        $status = CardLogModel::insertGetId($ins_data);
        if($status)
        {
            $this->success('生成成功');
        }
        $this->error('生成失败，请重试');
    }
    
    public function activeVipCard()
    {
        if($this->auth->is_vip == 1)
        {
            $this->error('您已激活',[]);
        }
        $card = $this->request->post('card','');
        $code = $this->request->post('code','');
        if(!$card || !$code)
        {
            $this->error('请输入卡号、卡密',[]);
        }
        $check_where['card'] = $card;
        $check_where['code'] = $code;
        $sttaus=  CardCodeModel::where($check_where)->find();
        if(!$sttaus)
        {
            $this->error('激活码不存在',[]);
        }
        if($sttaus['is_exchange'] == '1')
        {
            $this->error('激活码已使用',[]);
        }
        //查找是否存在代理
        $agent_where['id'] = $sttaus['agent_id'];
        $check_agent = Usermodel::where($agent_where)->find();
        if($check_agent)
        {
            //获取上级
            //获取上级
            $agent_info = AgentLogModel::where('user_id',$check_agent['id'])->find();

            $log_where['user_id']= $this->auth->id;
            $log_where['agent_id']= $check_agent['id'];
            $log_where['tagent_id']= $agent_info?$agent_info['agent_id']:0;
            $log_where['createtime']= time();
            CardLogModel::insertGetId($log_where);
            Usermodel::where($agent_where)->setInc('regis',1);
        }
        $data['is_exchange'] = '1';
        $data['use_time'] = time();
        $data['user_id'] = $this->auth->id;

        CardCodeModel::where($check_where)->update($data);
        Usermodel::where('id',$this->auth->id)->update(['is_vip'=>1]);
        $this->success('激活成功',[]);
    }

}